package com.bbzn.device.client.controller;

import com.bbzn.device.client.vo.res.SupervisorsIndexVO;
import com.bbzn.device.client.aspect.OperationAnnotation;
import com.bbzn.device.client.constant.DeviceConstant;
import com.bbzn.device.client.dataobject.SystemParam;
import com.bbzn.device.client.exception.DefinitException;
import com.bbzn.device.client.service.CommonService;
import com.bbzn.device.client.service.DeviceService;
import com.bbzn.device.client.service.IndexService;
import com.bbzn.device.client.service.SystemParamByCacheService;
import com.bbzn.device.client.utils.BaseApiService;
import com.bbzn.device.client.utils.BaseResponse;
import com.bbzn.device.client.utils.MayiktJwtUtils;
import com.bbzn.device.client.utils.RedisUtils;
import com.bbzn.user.comment.User;
import com.bbzn.user.comment.res.UserRes;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/supervisorsSite")
@Slf4j
public class SupervisorsController extends BaseApiService {

    @Autowired
    private CommonService commonService;

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private SystemParamByCacheService systemParamByCacheService;

    @Autowired
    private IndexService indexService;

    /**
     * 返回主页数据
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getSupervisorsIndex", method = RequestMethod.POST)
    @OperationAnnotation(content = "获取监管人首页")
    public BaseResponse getSupervisorsIndex(HttpServletRequest request) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
//        String language = "CN";
//        Long userId = 1L;
        User query = new User();
        query.setId(userId);
        List<UserRes> users = commonService.getChildByUser(query);
        if(CollectionUtils.isEmpty(users)){
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_ID_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        SupervisorsIndexVO supervisorsIndexVO = indexService.parseSupervisorsIndexVO(users);
        return setResultSuccess(supervisorsIndexVO);
    }
}